package snmptest.gateway;

import com.isnmp.common.base.MyLog;
import com.isnmp.common.snmp.SNMPAPI;
import com.isnmp.common.snmp.SNMPFactory;
import com.isnmp.common.snmp.SNMPTarget;
import com.isnmp.common.snmp.mib.MibIfEntry;
import com.isnmp.common.snmp.mib.MibOspfStubAreaEntry;
import com.isnmp.common.snmp.mib.MibSystem;

public class SetGetTest {
    SNMPAPI snmpapi;
    SNMPTarget nodeParam;

    public SetGetTest() {
        try {
            MyLog.initLogParam("SNMPTest");

            SNMPFactory.init(1, null, 8088);

            this.snmpapi = SNMPFactory.getSNMPAPI();
            SNMPFactory.loadMib("OSPF-MIB");

            this.nodeParam = new SNMPTarget();
            this.nodeParam.nodeIP = "127.0.0.1";
            this.nodeParam.port = 8001;
            this.nodeParam.writeCommunity = "private";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testGetOID() {
        try {
            String v = this.snmpapi.getOIDValue("1.3.6.1.2.1.2.2.1.2.1.8", this.nodeParam);
            System.out.println(v);
        } catch (Exception ex) {
            if (ex.getMessage().indexOf(SNMPAPI.MessageNoSuchOID) >= 0) {
                System.out.println("SNMP可达，但OID不存在");
            }

            ex.printStackTrace();
        }
    }

    public void testGetNextOID() {
        try {
            String v = this.snmpapi.getNextOIDValue("1.3.6.1.2.1.1.2.0", this.nodeParam);
            System.out.println(v);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testUpdateSystem() {
        try {
            MibSystem mib = new MibSystem();
            mib.setSysName("objectsnmp");
            mib.setSysUpTime(111L);
            this.snmpapi.update(mib, this.nodeParam);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testIf() {
        try {
            MibIfEntry entry = new MibIfEntry();
            entry.setIfIndex(1);
            Object obj = this.snmpapi.getMibObject(entry, this.nodeParam);
            System.out.println(obj);
            entry = (MibIfEntry) obj;
            entry.setIfAdminStatus(1);
            this.snmpapi.update(entry, this.nodeParam);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testAddTableOspf() {
        try {
            MibOspfStubAreaEntry ospf = new MibOspfStubAreaEntry();
            ospf.setOspfStubAreaId("192.168.9.0");
            ospf.setOspfStubTOS(3);
            ospf.setOspfStubStatus(4);

            ospf.setOspfStubMetric(1);
            ospf.setOspfStubMetricType(2);
            this.snmpapi.addTableRow(ospf, this.nodeParam);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testUpdateTableOspf() {
        try {
            MibOspfStubAreaEntry ospf = new MibOspfStubAreaEntry();
            ospf.setOspfStubAreaId("192.168.9.0");
            ospf.setOspfStubTOS(3);
            ospf.setOspfStubStatus(1);

            ospf.setOspfStubMetric(3);
            ospf.setOspfStubMetricType(3);
            this.snmpapi.update(ospf, this.nodeParam);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void testdelTableOspf() {
        try {
            MibOspfStubAreaEntry ospf = new MibOspfStubAreaEntry();
            ospf.setOspfStubAreaId("192.168.9.0");
            ospf.setOspfStubTOS(3);
            ospf.setOspfStubStatus(6);

            ospf.setOspfStubMetric(3);
            ospf.setOspfStubMetricType(3);
            this.snmpapi.delTableRow(ospf, this.nodeParam);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SetGetTest test = new SetGetTest();
            test.testGetOID();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}